package com.yanqu.road.dao.impl.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.config.clinic.DepartmentLv;
import com.yanqu.road.entity.config.clinic.DiseaseInfo;
import com.yanqu.road.entity.config.clinic.PatientInfo;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2021/3/3 15:25
 */
public class ClinicConfigDaoImpl extends TempDao {

    public Map<Integer, Map<Integer, DepartmentLv>> getDepartmentLvMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer, DepartmentLv>> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_p_clinic_department";
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                DepartmentLv data = new DepartmentLv();
                data.setDepartmentId(rs.getInt("id"));
                data.setType(rs.getInt("type"));
                data.setLevel(rs.getInt("level"));
                data.setUpCost(rs.getString("consume"));
                data.setUnlockDiseaseId(rs.getInt("unclockDisease"));
                data.setScore(rs.getInt("score"));
                data.setValue(rs.getInt("medicalVal"));
                data.setEventId(rs.getInt("triggerEvent"));
                if (!dataMap.containsKey(data.getType())) {
                    dataMap.put(data.getType(), new ConcurrentHashMap<>());
                }
                dataMap.get(data.getType()).put(data.getLevel(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public Map<Integer, DiseaseInfo> getDiseaseInfoMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, DiseaseInfo> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_p_clinic_disease";
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                DiseaseInfo data = new DiseaseInfo();
                data.setDiseaseId(rs.getInt("id"));
                data.setOccupation(rs.getInt("occupation"));
                data.setDepartmentList(rs.getString("hospitalLists"));
                data.setSkill(rs.getInt("skill"));
                data.setWeight(rs.getInt("weight"));
                data.setRewardWeight(rs.getString("specialRewards"));
                dataMap.put(data.getDiseaseId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public Map<Integer, PatientInfo> getPatientInfoMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, PatientInfo> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_p_clinic_patient";
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                PatientInfo data = new PatientInfo();
                data.setPatientId(rs.getInt("id"));
                data.setUnlockScore(rs.getInt("unlock"));
                data.setValueAdd(rs.getInt("effect"));
                data.setWeight(rs.getInt("weight"));
                data.setSort(rs.getInt("sort"));
                dataMap.put(data.getPatientId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }
}
